package com.web.controller.student;

import com.common.apiresult.CommonResult;
import com.common.apiresult.ResultCode;
import com.common.domain.dto.MaintainDTO;
import com.common.domain.vo.DormPropertyVO;
import com.common.domain.vo.MainTainVO;
import com.service.serviceInterface.DormPropertyService;
import com.service.serviceInterface.MaintainService;
import com.service.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @author liuxuhuang
 * @date 2023年04月04日 18:27
 * @desc 宿舍财产控制器
 */
@RestController(value = "studentDormPropertyController")
@Api(value = "dormProperty", tags = "宿舍财产控制器")
@RequestMapping(value = "/student/dormProperty")
@Slf4j
public class DormPropertyController {

    @Resource
    private MaintainService maintainService;
    @Resource
    private DormPropertyService dormPropertyService;
    @Resource
    private SecurityUtil securityUtil;

    @GetMapping
    @ApiOperation(value = "获取所在宿舍财产信息")
    public CommonResult get() {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        Set<DormPropertyVO> list = dormPropertyService.getData(userId);
        return list.size() > 0 ? CommonResult.success(list) : CommonResult.failed(ResultCode.RETURNNULL);
    }

    @GetMapping(value = "maintainRecord")
    @ApiOperation(value = "获取所在宿舍财产历史维修信息")
    public CommonResult getMaintainRecord() {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        Set<MainTainVO> list = dormPropertyService.getMaintainRecord(userId);
        return list.size() > 0 ? CommonResult.success(list) : CommonResult.failed(ResultCode.RETURNNULL);
    }

    @PostMapping
    @ApiOperation(value = "新增所在宿舍财产维修申请单")
    public CommonResult addMaintain(@RequestBody MaintainDTO dto) {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        return maintainService.add(dto, userId);
    }

    @PutMapping
    @ApiOperation(value = "修改所在宿舍财产维修申请单")
    public CommonResult updateMaintain(@RequestBody @Validated MaintainDTO dto) {
        return maintainService.updateMainTain(dto);
    }


}
